package com.ruoyi.web.controller.sxt;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.sxt.domain.SxtYonghu;
import com.ruoyi.sxt.domain.SxtYonghuJuese;
import com.ruoyi.sxt.service.ISxtJueseService;
import com.ruoyi.sxt.service.ISxtYonghuService;
import com.ruoyi.system.domain.SysUserRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 角色信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-26
 */
@Controller
@RequestMapping("/sxt/juese")
public class SxtJueseController extends BaseController
{
    private String prefix = "sxt/juese";

    @Autowired
    private ISxtJueseService sxtJueseService;

    @Autowired
    private ISxtYonghuService sxtYonghuService;

    @RequiresPermissions("sxt:juese:view")
    @GetMapping()
    public String juese()
    {
        return prefix + "/juese";
    }

    /**
     * 查询角色信息列表
     */
    @RequiresPermissions("sxt:juese:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SxtJuese sxtJuese)
    {
        startPage();
        List<SxtJuese> list = sxtJueseService.selectSxtJueseList(sxtJuese);
        return getDataTable(list);
    }

    /**
     * 导出角色信息列表
     */
    @RequiresPermissions("sxt:juese:export")
    @Log(title = "角色信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SxtJuese sxtJuese)
    {
        List<SxtJuese> list = sxtJueseService.selectSxtJueseList(sxtJuese);
        ExcelUtil<SxtJuese> util = new ExcelUtil<SxtJuese>(SxtJuese.class);
        return util.exportExcel(list, "角色信息数据");
    }

    /**
     * 新增角色信息
     */
    @RequiresPermissions("sxt:juese:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存角色信息
     */
    @RequiresPermissions("sxt:juese:add")
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SxtJuese sxtJuese)
    {
        SxtJuese juese = new SxtJuese();
        juese.setRoleName(sxtJuese.getRoleName());
        List<SxtJuese> sxtJueseList = sxtJueseService.selectSxtJueseList(juese);
        if(sxtJueseList != null && sxtJueseList.size() > 0){
           return error("新增角色'"+sxtJuese.getRoleName()+"'失败,该角色已存在");
        }
        return toAjax(sxtJueseService.insertSxtJuese(sxtJuese));
    }

    /**
     * 修改角色信息
     */
    @RequiresPermissions("sxt:juese:edit")
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        SxtJuese sxtJuese = sxtJueseService.selectSxtJueseByRoleId(roleId);
        mmap.put("sxtJuese", sxtJuese);
        return prefix + "/edit";
    }

    /**
     * 修改保存角色信息
     */
    @RequiresPermissions("sxt:juese:edit")
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SxtJuese sxtJuese)
    {
        return toAjax(sxtJueseService.updateSxtJuese(sxtJuese));
    }

    /**
     * 删除角色信息
     */
    @RequiresPermissions("sxt:juese:remove")
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sxtJueseService.deleteSxtJueseByRoleIds(ids));
    }

    /**
     * 分配用户
     */
    @RequiresPermissions("system:role:edit")
    @GetMapping("/authUser/{roleId}")
    public String authUser(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
//        if("1".equalsIgnoreCase(sxtJueseService.selectSxtJueseByRoleId(roleId).getStatus())){
//            throw new ServiceException("该角色已经停用!");
//        }
        mmap.put("role", sxtJueseService.selectSxtJueseByRoleId(roleId));
        return prefix + "/authUser";
    }

    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/authUser/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(SxtJuese juese)
    {
        startPage();
        List<SxtYonghu> sxtYonghuList = sxtYonghuService.selectYonghuByJueseId(juese);
        return getDataTable(sxtYonghuList);
    }

    /**
     * 选择用户
     */
    @RequiresPermissions("system:role:list")
    @GetMapping("/authUser/selectUser/{roleId}")
    public String selectUser(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("role", sxtJueseService.selectSxtJueseByRoleId(roleId));
        return prefix + "/selectUser";
    }

    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/authUser/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(SxtJuese sxtJuese)
    {
        startPage();
        List<SxtYonghu> sxtYonghuList = sxtYonghuService.selectUnYonghuByJueseId(sxtJuese);
        return getDataTable(sxtYonghuList);
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/selectAll")
    @ResponseBody
    public AjaxResult selectAuthUserAll(Long roleId, String userIds)
    {
        return toAjax(sxtJueseService.insertAuthUsers(roleId, userIds));
    }

    /**
     * 取消授权
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancel")
    @ResponseBody
    public AjaxResult cancelAuthUser(SxtYonghuJuese sxtYonghuJuese)
    {
        return toAjax(sxtJueseService.deleteSxtYonghuJueseInfo(sxtYonghuJuese));
    }
}
